<?php
class RegisterAction extends Action{
	public function __construct()
	{
		parent::__construct();
	}
	
	protected function init()
	{

	}
	public function display()
	{
		$this->smarty->display("register.tpl");
	}
	public function doView()
	{
		if($this->session->getAttr("uid") != null){
			$this->forward("Profile");
		}

		$sch = new SchoolModel();
		$schools = $sch->findAll();

		$this->smarty->assign("schools", $schools);
		$this->display();
	}
	public function register()
	{
		$user = new UserModel();

		$signatures = array(
			'truename' => array('name' => '真实姓名', 'required'=>true, 'type' => 'string'),
			'sex' => array('name' => '性别', 'required'=>true, 'type' => 'string'),
			'user_type' => array('name' => '身份', 'required'=>true, 'type' => 'string'),
			'degree' => array('required'=>false, 'type' => 'string'),
			'school_id' => array('required'=>false, 'type' => 'string'),
			'email' => array('name'=>'电子邮件', 'required'=>true, 'type' => 'string'),
			'password' => array('name' => '密码', 'required'=>true, 'type' => 'string'),
			'password2' => array('required'=>false, 'type' => 'string'),
			'privacy' => array('required'=>false, 'type' => 'string'),
			'checkcode' => array('name' => '验证码', 'required' => true, 'type' => 'string')
			);

		$failureStr = sanitize_vars($this->post, $signatures);

		if($this->post['checkcode'] != $this->session->getAttr('CheckCode'))
		{
			echo $this->post['checkcode'] . ":" . $this->session->getAttr('CheckCode');
			$this->showMessage("验证码错误!");
		}

		if($failureStr != "")
		{
			$this->showMessage("对不起，必填字段 ". $signatures[$failureStr]['name']. " 没有填写。");
		}
		else{
			if($this->post['password'] != $this->post['password2']){
				echo $this->post['password']. ":" . $this->post['password2'];
				$this->showMessage("两次输入的密码不一致!");
			}else{
				$user->create();
				$user->password = md5($user->email . $user->password);
				if($user->add())
				{
					$msg = "注册成功! 请登录。";
					mkdir(UPLOAD_DIR . "/". $user->email);
					mkdir(UPLOAD_DIR . "/". $user->email. "/thumbs");
				}else{
					$msg = "注册失败! 请联系管理员。";
				}
				$this->showMessage($msg);
			}
		}
	}
	public function showMessage($msg)
	{
		$this->smarty->assign("msg", $msg);
		$this->smarty->display("register_result.tpl");
		exit;
	}
};
?>
